/**author: wanghua    date: 2017-9-6**/
(function($) {
	$.fn.dkform = function(){
		var self = this;
//增加提示信息行(<tr class='tipbox'>)
		var appendTipRow = function(){
			$("tr", $(self)).each(function(){
				var thisTr = $(this);
				//thisTr.attr("hasTip","true");//判断是否已经添加了tip tr
				if(thisTr.attr("ignoreTip") == "true")return;//不需要为此tr增加tip tr

				var tipTrHtml = "<tr class='tip-box'>";
				$("td", thisTr).each(function(){
					var thisTd = $(this);
					var inputId = $("input,select,textarea", thisTd).attr("id");
					var colspanHtml = "";
					var colspan = parseInt(thisTd.attr("colspan") || 1);
					if(colspan > 1) colspanHtml = 'colspan="'+colspan+'"';
					var tdHtml = '';
					if(inputId){
						tdHtml = '<td @{colspan}><div class="has-tip has-feedback"><span class="tip_icon glyphicon glyphicon-info-sign" style="display:none;"></span><span id="tip_@{id}" class="help-block"></span></div></td>';
					}else{
						tdHtml = '<td @{colspan}></td>';
					}
					tdHtml = tdHtml.replace("@{colspan}", colspanHtml);
					tdHtml = tdHtml.replace("@{id}", inputId);
					tipTrHtml += tdHtml;
				});
				tipTrHtml += "</tr>";
				thisTr.after(tipTrHtml);
			});
		};

		/**/
		var showFeedbackIcon = function(){
			$("input", self).each(function(){
				var thisInput = $(this);
				var iconClass = thisInput.attr("icon");
				if(iconClass){
					thisInput.attr("icon", "");	 //避免重复构造
					var inputId = thisInput.attr("id");
					var divId = "pdiv_" + inputId;
					var template = '<div id="@{divId}"  class="has-feedback"><span class="'+iconClass+' form-control-feedback"></span></div>'; //iconClass: glyphicon glyphicon-user
					var divHtml = template.replace("@{divId}", divId);
					thisInput.before(divHtml);
					thisInput.prependTo($("#"+divId));
				}
			});
		};

		var addTipListener = function(){
			$("input,select,textarea").on( "focus", function() { //得到焦点的时候
				self.showTip($(this).attr("id"));
			});

			$("input,select,textarea").on( "blur", function() {	 //失去焦点的时候
				self.hideTip($(this).attr("id"));
			});
			/*$(this).on( "focus", "input,select,textarea", function() { //得到焦点的时候
				this.showTip($(this).attr("id"));
			});

			$(this).on( "blur", "input,select,textarea", function() {	 //失去焦点的时候
				this.hideTip($(this).attr("id"));
			});*/
		};
		/**显示计量单位*/
		var showUnit = function(){
			$("input", self).each(function(){
				var thisInput = $(this);
				var unit = thisInput.attr("unit");
				if(unit){
					var unithtml = '<span class="unit">'+ unit +'</span>';
				}
				thisInput.after(unithtml);
			});
		};
		
		this.init = function(){
			appendTipRow();
			showFeedbackIcon();
			showUnit();
			addTipListener();
		};
		
		/**msg可以为空， 默认从input的 tip 属性取值*/
		this.showTip = function(inputId, msg){
			var jqInput = $("#"+inputId);
			var jqTip = $("#tip_"+inputId);
			if(jqTip.hasClass("err-msg"))return;//正在显示错误提示，则不显示普通提示
			if(!msg){
				msg = jqInput.attr("tip");
			}
			if(!msg) msg = "";//避免显示 undefined
			
			jqTip.removeClass("err-msg").addClass("tip-msg");
			jqTip.html(msg);
		};

		/**提出普通提示信息*/
		this.hideTip = function(inputId){
			var jqInput = $("#"+inputId);
			var jqTip = $("#tip_"+inputId);
			if(jqTip.hasClass("tip-msg")){
				jqTip.removeClass("tip-msg")
				jqTip.html("");
			}
		}

		this.showError = function(inputId, msg){
			var jqInput = $("#"+inputId);
			if(!msg){
				msg = jqInput.attr("errmsg");
			}
			if(!msg) msg = "";//避免显示 undefined
			var jqTip = $("#tip_"+inputId);
			jqTip.removeClass("tip-msg").addClass("err-msg");
			jqTip.html(msg);
		};

		/**提出错误提示信息*/
		this.hideError = function(inputId){
			var jqInput = $("#"+inputId);
			var jqTip = $("#tip_"+inputId);
			if(jqTip.hasClass("err-msg")){
				jqTip.removeClass("err-msg")
				jqTip.html("");
			}
		}


		return this;
	}
})(jQuery);


$(window).load(function() {
	$('.dkTab:first').focus();//  .dkTab 表示这是一个接受tab键跳转的控件
	var $dkTab = $('.dkTab');
	$dkTab.bind('keydown', function (e) {
		var key = e.which;
		if (key == 13  && !$(this).hasClass('submitButton')) {
			e.preventDefault();
			var nextIdx = $dkTab.index(this) + 1;
			if($(this).hasClass('bootstrap-select-searchinput')) {
				//nextIdx += 1;				 
			}
			$(".dkTab:eq(" + nextIdx + ")").focus();
		}
	});
	
	$('.submitButton').bind('keydown', function (e) {//在保存按钮上按tab键，回到第一个输入框
		var key = e.which;
		if (key == 9) {
			$('.dkTab:first').focus();
			return false;
		}
	});
	
	$('.submitButton').bind('click', function (e) {//点击保存按钮之后，跳转到第一个输入框，减少重复提交的误操作
		$('.dkTab:first').focus();
	});
	
	$(document).keydown(function(){ //页面上按ctrl+回车键的时候，跳转到保存按钮上。也可以改为直接提交。
		if(window.event.ctrlKey && window.event.keyCode == 13){ 
			$('.submitButton').focus();
			//$('.submitButton').click();//直接提交
		}
	});
	 
});